﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using YArchitech.LIB;

namespace YArchitech.Controls
{
	public class XMLUserSetting
	{
		public XMLUserSetting()
		{
			this.xmlPath = Path.Combine(Product.UserDataLocation, "HYAirSystemSetting.xml");
			if (!File.Exists(this.xmlPath))
			{
				File.Copy(Path.Combine(Product.DataLocation, "HYAirSystemSetting.xml"), this.xmlPath);
			}
			this.xmlDoc = XMLOperating.LoadXml(this.xmlPath);
		}

		public string GetValueFromDic(string name, string defaultvalue, Dictionary<string, string> dic)
		{
			string result;
			try
			{
				if (dic.FirstOrDefault((KeyValuePair<string, string> q) => q.Key == name).Value.ToString() == "")
				{
					result = defaultvalue;
				}
				else
				{
					result = dic.FirstOrDefault((KeyValuePair<string, string> q) => q.Key == name).Value.ToString();
				}
			}
			catch
			{
				result = defaultvalue;
			}
			return result;
		}

		public List<string[]> ReadPipingFittingSetInfo(string nodeName)
		{
			XmlNode xmlNode = this.xmlDoc.ChildNodes[0];
			XmlNodeList childNodes = this.xmlDoc.SelectSingleNode(xmlNode.Name).ChildNodes;
			XmlNodeList xmlNodeList = null;
			List<string[]> list = new List<string[]>();
			bool flag = false;
			foreach (object obj in childNodes)
			{
				XmlElement xmlElement = (XmlElement)((XmlNode)obj);
				if (xmlElement.Name == nodeName)
				{
					xmlNodeList = xmlElement.ChildNodes;
					flag = true;
					break;
				}
			}
			if (flag)
			{
				IEnumerator enumerator = xmlNodeList.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj2 = enumerator.Current;
						XmlNode xmlNode2 = (XmlNode)obj2;
						list.Add(new string[]
						{
							xmlNode2.Name,
							xmlNode2.Value
						});
					}
					return list;
				}
			}
			return null;
		}

		public Dictionary<string, string> GetFormCurrentSystemType(string nodeName)
		{
			return new Dictionary<string, string>
			{
				{
					"CurrentSystemType",
					XMLOperating.GetNodeText(this.xmlDoc, nodeName, "CurrentSystemType")
				},
				{
					"SelectSystemType",
					XMLOperating.GetNodeText(this.xmlDoc, nodeName, "SelectSystemType")
				}
			};
		}

		public void WriteFormCurrentSystemType(string nodeName, Dictionary<string, string> dicSystemTypeSetting)
		{
			foreach (KeyValuePair<string, string> keyValuePair in dicSystemTypeSetting)
			{
				XMLOperating.EditNodeText(this.xmlPath, nodeName, keyValuePair.Key, keyValuePair.Value);
			}
		}

		public Dictionary<string, string> GetFormLocationSetting(string nodeName)
		{
			return new Dictionary<string, string>
			{
				{
					"X",
					XMLOperating.GetNodeText(this.xmlDoc, nodeName, "X")
				},
				{
					"Y",
					XMLOperating.GetNodeText(this.xmlDoc, nodeName, "Y")
				}
			};
		}

		public void WriteFormLocationSetting(string nodeName, Dictionary<string, string> dicLaySetting)
		{
			foreach (KeyValuePair<string, string> keyValuePair in dicLaySetting)
			{
				XMLOperating.EditNodeText(this.xmlPath, nodeName, keyValuePair.Key, keyValuePair.Value);
			}
		}

		public void WriteUserData(Dictionary<string, string> dic, string noteName)
		{
			foreach (KeyValuePair<string, string> keyValuePair in dic)
			{
				XMLOperating.EditNodeText(this.xmlPath, noteName, keyValuePair.Key, keyValuePair.Value);
			}
		}

		public void WriteValue(string parentName, string subName, string value)
		{
			XMLOperating.EditNodeText(this.xmlPath, parentName, subName, value);
		}

		public string ReadValue(string parentName, string subName)
		{
			return XMLOperating.GetNodeText(this.xmlDoc, parentName, subName);
		}

		private XmlDocument xmlDoc;

		private string xmlPath = "";
	}
}
